﻿using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Serilog;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Volo.Abp;
using WenABP.Customer.Domain.Data;

namespace WenABP.Customer.Migrator
{
    public class DBMigratorHostedService : IHostedService
    {
        public async Task StartAsync(CancellationToken cancellationToken)
        {
            using (var application = AbpApplicationFactory.Create<WenABPMigratorModule>(opthons=> {
                opthons.UseAutofac();
                opthons.Services.AddLogging(c=>c.AddSerilog());
            }))
            {
                application.Initialize();
                await application
                    .ServiceProvider
                    .GetRequiredService<CustomerStoreMigratorService>()
                    .MigrateAsync();
                application.Shutdown();
            }
        }

        public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
    }
}
